package com.icode.web.frontController.house;

import com.icode.core.dto.house.BuildingTypeDTO;
import com.icode.core.dto.house.HouseBaseDTO;
import com.icode.core.dto.house.HouseInfoDTO;
import com.icode.core.dto.manager.SearchHouseBaseDTO;
import com.icode.core.model.house.BuildingType;
import com.icode.core.shared.DataToJson;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-29
 * Time: 上午10:06
 * To change this template use File | Settings | File Templates.
 */
@Controller("houseBaseFrontController")
@RequestMapping(value = "/front/houseBase")
public class HouseBaseFrontController {
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "list/{type}", method = RequestMethod.GET)
    public String showHouseBaseList(@PathVariable String type, Model model) {
        List<HouseBaseDTO> houseBaseDTOs = houseService.findAllHouseBase(type);
        model.addAttribute("houseBaseList", houseBaseDTOs);
        model.addAttribute("houseBaseListSize",houseBaseDTOs.size());
        return "front/houseBaseListNew";
    }

    @RequestMapping(value = "showType/{houseBaseGuid}")
    public void showHouseType(@PathVariable String houseBaseGuid, HttpServletResponse response) {
        List<BuildingType> buildingTypes = houseService.findBuildingTypeByHouseBase(houseBaseGuid);
        List<BuildingTypeDTO> buildingTypeDTOs = BuildingTypeDTO.toDTOs(buildingTypes);
        DataToJson.toBuildTypes(buildingTypeDTOs, response);
    }

    @RequestMapping(value = "showHouseInfo/{buildingTypeGuid}", method = RequestMethod.GET)
    public String showHouseInfo(@PathVariable String buildingTypeGuid, Model model) {
        HouseInfoDTO houseInfoDTO = houseService.findHouseInfoByBuildingTypeGuid(buildingTypeGuid);
        model.addAttribute("houseInfo", houseInfoDTO);
        return "front/houseInfoNew1";
    }

    @RequestMapping(value = "search", method = RequestMethod.POST)
    public String search(String area, String price, String theme, String productType, Model model) {
        List<HouseBaseDTO> houseBaseDTOs = houseService.findHouseBaseByMoreCondition(area, price, theme, productType);
        model.addAttribute("houseBaseList", houseBaseDTOs);
        model.addAttribute("houseBaseListSize",houseBaseDTOs.size());
        return "front/houseBaseData";
    }

    @RequestMapping(value = "searchFromIndex/{area}/{price}/{theme}/{productType}")
    public String searchFromIndex(@PathVariable String area, @PathVariable String price, @PathVariable String theme, @PathVariable String productType, Model model) {
        return "redirect:/front/houseBase/list/" + area + "/" + price + "/" + theme + "/" + productType;
    }

    @RequestMapping(value = "list/{area}/{price}/{theme}/{productType}", method = RequestMethod.GET)
    public String showHouseBaseList(@PathVariable String area, @PathVariable String price, @PathVariable String theme, @PathVariable String productType, Model model) {
        List<HouseBaseDTO> houseBaseDTOs = houseService.findHouseBaseByMoreCondition(area, price, theme, productType);
        model.addAttribute("houseBaseList", houseBaseDTOs);
        model.addAttribute("houseBaseListSize",houseBaseDTOs.size());
        return "front/houseBaseListNew";
    }

}
